<template>
  <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body :close-on-click-modal="false" v-dialogDrag>

    <div class="app-container">
      <el-table border v-loading="loading" :data="dataList" ref="selectTable"
                @select="selectCur"
                show-summary :summary-method="getSummaries" :max-height="tableMaxHeight">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="订单编号" align="center" prop="projectNo" />
        <el-table-column label="开票单位" align="center" prop="customerName" />
        <el-table-column label="开票单位编码" align="center" prop="taxNo"/>
        <el-table-column label="开票金额" align="center" prop="amount"/>
        <el-table-column label="核销状态" align="center" prop="fundCallback">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.invoice_back_status" :value="scope.row.fundCallback"/>
          </template>
        </el-table-column>
        <el-table-column label="已核销金额" align="center" prop="pinMoney"/>
        <el-table-column label="剩余核销金额" align="center" prop="pinRemain"/>
        <el-table-column label="业务员" align="center" prop="saleName" show-overflow-tooltip/>
        <el-table-column label="申请人" align="center" prop="drawerName"/>
        <el-table-column sortable label="开票日期" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.invoiceDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>

    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定 核 销</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {accAdd, deleteAction, downloadFile, getAction} from '@/api/manage'

export default {
  name: "GoodsInvoiceUnWriteOff",
  dicts: ['fund_callback', 'yes_no', 'company', 'invoice_back_status', 'invoice_apply_status', 'invoice_type'],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      selectedRowId: null,
      // 总条数
      total: 0,
      // 发票管理表格数据
      dataList: [],
      dateRange: [],
      receiptId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        type: null,
        no: null,
        amount: null,
        amountReal: null,
        remaining: null,
        customerId: null,
        customerName: null,
        projectName: null,
        projectNo: null,
        taxNo: null,
        invoiceDate: null,
        invoiceCompany: null,
        fundCallback: "whx",
        backDate: null,
        drawerId: null,
        drawerName: null,
        saleId: null,
        saleName: null,
        files: null,
        status: "2",
        params: {}
      },
      url: {
        list: '/goodsInvoice/goodsInvoice/listWhx',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {

  },
  methods: {
    show(row) {
      this.open = true;
      this.title = "未核销发票";
      this.receiptId = row.id;
      this.queryParams.customerId = row.customerId;
      this.queryParams.projectNo = row.orderCode;
      this.queryParams.projectId = row.orderId;
      this.queryParams.amount = row.money;
      this.getList();
    },
    /** 查询发票管理列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange, 'InvoiceDate');
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    selectCur(val, row) {
      console.log(val, 'val')
      console.log(row, 'row')
      this.selectedRowId = row.id;
      this.$refs.selectTable.clearSelection()
      this.$refs.selectTable.toggleRowSelection(row, true)
    },


    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (column.property === "amount"
          || column.property === 'amountReal'
          || column.property === 'remaining'
          || column.property === 'pinMoney'
          || column.property === 'pinRemain'
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    submitForm() {
      let that = this;
      if (this.selectedRowId == null) {
        this.$message.warning("请选择要核销的发票")
        return;
      }
      this.$confirm('是否核销发票?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return getAction("/financeInvoiceReceipt/financeInvoiceReceipt/writeOff", {
          id: that.receiptId,
          invoiceId: that.selectedRowId
        });
      }).then(() => {
        this.$modal.msgSuccess("核销成功");
        this.open = false;
        this.$emit('ok');
      })
    }


  }
};
</script>
<style lang="scss" scoped>
// 将全选项隐藏
::v-deep .el-table__header-wrapper .el-checkbox {
  display: none;
}
</style>

